/****************************************************************************
*                                                                           *
* singleton.h -- template singleton implementation                          *
*                                                                           *
* ALaRI 2008												                *
*                                                                           *
****************************************************************************/
#pragma once

#if !defined(__SINGLETON_H)
#define __SINGLETON_H

#include "defs.h"

template <class _T> 
class CSingleton
{
public : 
	/****************************************************************************
	* Public interface															*
	****************************************************************************/

protected:
	/****************************************************************************
	* Protected Construction/Destruction										*
	****************************************************************************/
	static _T* getInstance(bool bNew = 0);
	static void destroyInstance();

	CSingleton()
	{};
	
	virtual ~CSingleton() { m_pInstance = NULL; };

private : 
	/****************************************************************************
	* Private data																*
	****************************************************************************/
	static _T	*m_pInstance;

};

template <class _T> 
_T* CSingleton<_T>::getInstance(bool bNew /*= 0*/)
{
	if (true == bNew)
		{
		if (NULL != m_pInstance) 
			destroyInstance();
		m_pInstance = new _T();
	}
	else 
	{
		if (NULL == m_pInstance)
			m_pInstance = new _T();
	}
	assert(m_pInstance);

	return m_pInstance;
}

template <class _T> 
void CSingleton<_T>::destroyInstance()
{
	SafeFree(m_pInstance);
}

template <class _T> _T* CSingleton<_T>::m_pInstance = NULL;


#endif /* __SINGLETON_H */
/****************************************************************************
* End of file																*
****************************************************************************/
